package com.airnews.ticket.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.airnews.ticket.R;
import com.airnews.ticket.adapter.CityListViewAdapter;
import com.airnews.ticket.ui.flight.ContactorAdd;
import com.mobclick.android.MobclickAgent;

public class CityList extends BaseActivity implements OnItemClickListener {
	private String[] mString;
	private String[] nString;
	private String[] eString;
	
	private CityListViewAdapter mla;
	private ListView lv;
	private String comcity;
	
	private Toast toast;
	
	private static List<HashMap<String,String>> cityList = new ArrayList<HashMap<String,String>> ();
	private AutoCompleteTextView  searchCity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.city);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
		activityList.add(this);
		
		searchCity = (AutoCompleteTextView) findViewById(R.id.searchCity);
		
		lv = (ListView) findViewById(R.id.clist);
		
		mString = getResources().getStringArray(R.array.chotcitys);
		nString = getResources().getStringArray(R.array.searchcitys);//自动识别用的例表
		
		eString = new String[nString.length];
		for(int i=0;i<nString.length;i++){
			String s = nString[i];
			if(s.indexOf("-")!=-1){
				eString[i] = s.split("-")[0];
			}
		}
		
		
		mla = new CityListViewAdapter(this,R.layout.city_row,mString);         
		lv.setAdapter(mla); 
		lv.setOnItemClickListener(this);
		
//		mla = new ArrayAdapter<String>(this,
//				R.layout.city_row, mString);
//		lv.setAdapter(mla);
//		lv.setOnItemClickListener(this);
		
		
//		Button btn = (Button) findViewById(R.id.hotcity);
//		btn.setOnClickListener(this);
//		Button btn2 = (Button) findViewById(R.id.homecity);
//		btn2.setOnClickListener(this);
		
		//创建适配器        
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,nString); 
		searchCity.setAdapter(adapter);        
		//设置输入多少字符后提示，默认值为2        
		searchCity.setThreshold(1);
		searchCity.setOnItemClickListener(this);
		

	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		comcity = getIntent().getStringExtra("comcity");
		
		Log.i("onItemClick", String.valueOf(arg0.getClass()));
		String city = "";
		if(String.valueOf(arg0.getClass()).equals("class android.widget.AutoCompleteTextView$DropDownListView")){
			city = searchCity.getText().toString();
		} else {
			city = mString[position];
		}
		if(city.indexOf("-")!=-1){
			city = city.split("-")[1];
		}
		
		Log.i("onItemClick comcity", comcity);
		Log.i("onItemClick city", city);
		if(!city.matches("[\\u4e00-\\u9fbb]+") || city.equals("热点")){
			toast = Toast.makeText(CityList.this, "请选择城市", Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			return;				
		} else {
			Intent intent = getIntent();
			Bundle bundle=new  Bundle();
			bundle.putString("city", city);
			intent.putExtras(bundle);
			setResult(RESULT_OK, intent);
			finish();
		}
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
